package com.jp.knowledge.my.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.google.gson.JsonObject;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.my.adapter.MessageSystemAdapter;
import com.jp.knowledge.my.model.MessageSystemModel;
import com.jp.knowledge.util.ResultSubscriber;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/5/10.
 */

public class MessageDetailActivity extends SlidingActivity implements CanRefreshLayout.OnRefreshListener, CanRefreshLayout.OnLoadMoreListener, ResultSubscriber.OnResultListener {

    private static final int REFRESH_LIST_CODE = 0x01;
    private static final int LOAD_MODE_LIST_CODE = 0x02;

    @ViewInject(R.id.refresh)
    private CanRefreshLayout refreshLayout;
    @ViewInject(R.id.can_content_view)
    private RecyclerView contentRv;

    private int type;
    private String id;
    private int page;
    private List<MessageSystemModel> messageSystemModelList;
    private MessageSystemAdapter messageSystemAdapter;

    @Override
    protected int getResourceId() {
        return R.layout.activity_message_detail;
    }

    @Override
    protected void init() {
        type = getIntent().getIntExtra("type", 11);
        id = getIntent().getStringExtra("id");
        page = 1;

        messageSystemModelList = new ArrayList<>();
        messageSystemAdapter = new MessageSystemAdapter(mContext, messageSystemModelList);

        initTitleBar();
        initView();

        refreshLayout.autoRefresh();
    }

    private void initTitleBar(){
        if(type == 11)
            topName.setText("公告");
        else if(type == 12)
            topName.setText("订单通知");
        else if(type == 13)
            topName.setText("任务通知");
        else
            topName.setText("消息");
        leftIcon.setImageResource(R.mipmap.arrow_white_left);
        rightIcon.setVisibility(View.GONE);
        leftIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initView(){
        contentView.setBackgroundColor(getResources().getColor(R.color.gray_bg));
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadMoreListener(this);
        contentRv.setHasFixedSize(true);
        contentRv.setLayoutManager(new LinearLayoutManager(mContext));
        contentRv.setAdapter(messageSystemAdapter);
    }

    private void getData(int page){
        JsonObject param = new JsonObject();
        param.addProperty("id", id);
        param.addProperty("type", type);
        param.addProperty("page", page);
        HttpHelper.getInstance(mContext)
                .messageSystemList(param, (page == 1) ? REFRESH_LIST_CODE : LOAD_MODE_LIST_CODE, this);
    }

    @Override
    public void onRefresh() {
        page = 1;
        getData(page);
    }

    @Override
    public void onLoadMore() {
        getData(++page);
    }

    @Override
    public void onStart(int requestType) {}
    @Override
    public void onCompleted(int requestType) {
        if(requestType == REFRESH_LIST_CODE)
            refreshLayout.refreshComplete();
        else if(requestType == LOAD_MODE_LIST_CODE)
            refreshLayout.loadMoreComplete();
    }
    @Override
    public void onError(int requestType) {
        onCompleted(requestType);
    }
    @Override
    public void onNext(IModel data, int requestType) {
        if(data.getErrcode() != 0){
            ToasUtil.toast(mContext, data.getMessage());
            onError(requestType);
        } else if(requestType == REFRESH_LIST_CODE || requestType == LOAD_MODE_LIST_CODE){
            if(requestType == REFRESH_LIST_CODE)
                messageSystemModelList.clear();
            List<MessageSystemModel> messageSystemModels = data.getList(MessageSystemModel.class);
            if(messageSystemModels != null && messageSystemModels.size() > 0)
                messageSystemModelList.addAll(messageSystemModels);
            messageSystemAdapter.updateAdapter(messageSystemModelList);
        }
    }
}
